<script>

import ActApplyBtn from '@views/flowable/components/ActApplyBtn.vue'
import ActHistoricDetailBtn from '@views/flowable/components/ActHistoricDetailBtn.vue'
import { stopProcess } from '@views/flowable/api/process'
import { revokeProcess } from '@views/flowable/api/finished'

export default {
  name: 'ApprovalButtons',
  components: { ActHistoricDetailBtn, ActApplyBtn },
  props: {
    // 1 需修改 2 审批中 3 已审批
    status: {
      type: Number,
      default: 1
    },
    dataId: {
      type: String,
      default: '',
      required: true
    },
    serviceName: {
      type: String,
      default: '',
      required: true
    },
    variables: {
      type: Object,
      default: {}
    }
  },
  methods: {
    success() {
      this.$emit('success')
    },
    handleStop() {
      if (!this.variables) {
        return
      }
      if (!this.variables.processInstanceId) {
        return
      }
      const params = {
        instanceId: this.variables.processInstanceId
      }
      stopProcess(params).then((res) => {
        this.$message.success(res.message)
        this.success()
      })
    },
    handleCancel() {
      const params = {
        dataId: this.variables.id,
        instanceId: this.variables.processInstanceId
      }
      revokeProcess(params).then(res => {
        this.$message.success(res.message)
        this.success()
      })
    }
  }
}
</script>

<template>
  <div>
    <act-apply-btn
      class="act-apply-btn"
      v-if="status === 1"
      @success="success"
      :data-id="dataId"
      :serviceName="serviceName"
      :variables="variables"
      text="重新提交"
    />
    <a-divider v-if="status === 1" type="vertical" />
    <act-historic-detail-btn v-if="[1,2,3].includes(status)" :data-id="dataId" />
    <a-divider v-if="status === 2" type="vertical" />
    <a-popconfirm title="确定取消申请吗?" v-if="status === 2" @confirm="() => handleStop()">
      <a>取消申请</a>
    </a-popconfirm>
    <!-- 备用 -->
    <!--
    <a-divider v-if="status === 2" type="vertical" />
    <a-popconfirm title="确定撤销吗?" v-if="status === 2" @confirm="() => handleCancel()">
      <a>撤销</a>
    </a-popconfirm>
    -->
  </div>
</template>

<style scoped lang="less">
.act-apply-btn {
  ::v-deep {
    .ant-btn {
      padding: 0;
    }
  }
}
</style>